package com.hfzy.ihk.common.core.elasticsearch.service;

import com.hfzy.ihk.common.core.elasticsearch.ElasticsearchSupport;
import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
 * Created by Administrator on 2018/2/27.
 *
 * 使用这个的时候需要注意：
 * 这些方法是没有配置redis缓存的
 *
 * @author wws
 */
public abstract class EsBaseServiceImpl<T> extends ElasticsearchSupport implements EsBaseService<T> {

    protected abstract ElasticsearchCrudRepository getReposity();

    @Override
    public boolean insert(T entity) {
         getReposity().save(entity);
        return true;
    }

    @Override
    public boolean insertBatch(List<T> entityList) {

        if(entityList == null || entityList.isEmpty()) {
            return false;
        }

        getReposity().saveAll(entityList);

        return true;
    }

    @Override
    public boolean insertBatch(List<T> entityList, int batchSize) {

        if(entityList == null || entityList.isEmpty()) {
            return false;
        }

        int count = 0;
        List<T> temp = new ArrayList<>(batchSize);
        for(T entity:entityList) {

            temp.add(entity);
            count++;

            if(count%batchSize==0) {
                getReposity().saveAll(temp);
                temp.clear();
                count = 0;
            }
        }

        if(temp.size()>0) {
            getReposity().saveAll(temp);
        }
        return true;
    }

    @Override
    public boolean delete(T entity) {
        getReposity().delete(entity);
        return true;
    }

    @Override
    public boolean deleteById(Serializable id) {
        getReposity().deleteById(id);
        return true;
    }

    @Override
    public boolean deleteBatchIds(List<? extends Serializable> idList) {

        if(idList == null || idList.isEmpty()) {
            return false;
        }

        for(Serializable id:idList) {
            deleteById(id);
        }

        return true;
    }

    @Override
    public boolean update(T entity) {
        getReposity().save(entity);
        return true;
    }

    @Override
    public boolean updateBatchById(List<T> entityList) {
        return insertBatch(entityList);
    }

    @Override
    public boolean updateBatchById(List<T> entityList, int batchSize) {
        return insertBatch(entityList,batchSize);
    }

    @Override
    public T selectById(Serializable id) {
        Optional<T> optional = getReposity().findById(id);
        try {
            return optional.get();
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public boolean existsById(Serializable id) {
        return  getReposity().existsById(id);
    }

    @Override
    public List<T> selectAll() {
        Iterable<T> rs = getReposity().findAll();
        if(rs == null) {
            return null;
        }
        return StreamSupport.stream(rs.spliterator(), false)
                .collect(Collectors.toList());
    }
}
